<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8">
  <title>设置我的资料</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
  <link rel="stylesheet" th:href="@{/style/custom.css}" media="all">
</head>
<body>

<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card" style="margin-top: 15px;">
        <div class="layui-card-header">设置我的资料</div>
        <div class="layui-card-body" pad15>
          <form class="layui-form" lay-filter="user-form-edit" id="user-form-edit" style="padding: 20px;">
            <input type="hidden" id="id" name="id" th:value="${user.id}"/>
            <input type="hidden" id="userId" name="userId" th:value="${user.userId}"/>

            <!--  <div class="layui-form-item">-->
            <!--    <label class="layui-form-label">头像</label>-->
            <!--    <div class="layui-input-inline">-->
            <!--      <div class="layui-upload">-->
            <!--        <button type="button" class="layui-btn" id="test1">上传图片</button>-->
            <!--        <div class="layui-upload-list">-->
            <!--          <img class="layui-upload-img" id="demo1">-->
            <!--          <p id="demoText"></p>-->
            <!--        </div>-->
            <!--        <div style="width: 95px;">-->
            <!--          <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="demo">-->
            <!--            <div class="layui-progress-bar" lay-percent=""></div>-->
            <!--          </div>-->
            <!--        </div>-->
            <!--      </div>-->
            <!--      &lt;!&ndash;      <input type="text" name="avatar" lay-verify="required" placeholder="请上传图片" autocomplete="off" class="layui-input">&ndash;&gt;-->
            <!--    </div>-->
            <!--&lt;!&ndash;    <button style="float: left;" type="button" class="layui-btn" id="user-form-upload">上传图片</button>&ndash;&gt;-->
            <!--  </div>-->
            <div class="layui-form-item">
              <div class="layui-col-xs6 layui-col-sm6 layui-col-md3">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                  <input type="text" name="name" th:value="${user.name}" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" lay-affix="clear">
                </div>
              </div>
              <div class="layui-col-xs6 layui-col-sm6 layui-col-md3">
                <label class="layui-form-label">登录名</label>
                <div class="layui-input-block">
                  <input type="text" name="username" th:value="${user.username}" lay-verify="required" readonly placeholder="请输入" autocomplete="off" class="layui-input" lay-affix="clear">
                </div>
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-col-xs6 layui-col-sm6 layui-col-md3">
                <label class="layui-form-label">手机号码</label>
                <div class="layui-input-block">
                  <input type="text" name="mobile" th:value="${user.mobile}" lay-verify="phone" placeholder="请输入" autocomplete="off" class="layui-input" lay-affix="clear">
                </div>
              </div>
              <div class="layui-col-xs6 layui-col-sm6 layui-col-md3">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                  <select name="gender" lay-filter="select" lay-verify="required">
                    <option value="">请选择</option>
                    <option th:each="map,idx:${genderList}" th:value="${map.key}" th:selected="${map.key == user.gender}" th:text="${map.value}"></option>
                  </select>
                </div>
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-col-xs6 layui-col-sm6 layui-col-md3">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                  <input type="text" name="email" th:value="${user.email}" lay-verify="email" placeholder="请输入" autocomplete="off" class="layui-input" lay-affix="clear">
                </div>
              </div>
              <div class="layui-col-xs6 layui-col-sm6 layui-col-md3">
                <label class="layui-form-label">生日</label>
                <div class="layui-input-block">
                  <input type="text" id="birthday" name="birthday" th:value="${#calendars.format(user.birthday,'yyyy-MM-dd')}" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-col-xs6 layui-col-sm6 layui-col-md3">
                <label class="layui-form-label">所属组织</label>
                <div class="layui-input-block " id="orgCode">
                </div>
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
                <label class="layui-form-label">岗位</label>
                <div class="layui-input-block" id="positions">
                </div>
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
                <label class="layui-form-label">角色</label>
                <div class="layui-input-block" id="roles">
                </div>
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
                <label class="layui-form-label">地址</label>
                <div class="layui-input-block">
                  <textarea name="address" th:text="${user.address}" placeholder="请输入内容" class="layui-textarea"></textarea>
                </div>
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
                <div class="layui-input-block" style="text-align:center;">
                  <input type="button" class="layui-btn layui-btn-normal" lay-submit lay-filter="LAY-user-front-submit" id="LAY-user-front-submit" value="提交">
                  <input type="button" class="layui-btn layui-btn-primary" lay-filter="LAY-user-front-reset" id="LAY-user-front-reset" value="重置">
                </div>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>

<script th:inline="javascript">
  let orgCode = [[${user.orgCode}]];
  let positions = [[${positions}]];
  let roles = [[${roles}]];
</script>
<script th:src="@{/layui/layui.js}"></script>
<script>
  layui.config({
    base: '/'
  }).extend({
    xmSelect: 'extend/xm-select/dist/xm-select'
  }).use(['index', 'xmSelect'], function () {
    let xmSelect = layui.xmSelect;
    let admin = layui.admin;
    let form = layui.form;
    let table = layui.table;
    let upload = layui.upload;

    upload.render({
      elem: '#user-form-upload'
      , url: ''
      , accept: 'images'
      , method: 'get'
      , acceptMime: 'image/*'
      , done: function (res) {
        $(this.item).prev("div").children("input").val(res.data.src)
      }
    });

    layui.laydate.render({
      elem: '#birthday' //指定元素
      , trigger: 'click'
      , format: 'yyyy-MM-dd'
      , type: 'date'
    });

    //所属组织
    let orgCode_select = xmSelect.render({
      el: '#orgCode',
      name: 'orgCode',
      layVerify: 'required',
      layVerType: 'msg',
      disabled: true,
      //显示为text模式
      model: {label: {type: 'text'}},
      //单选模式
      radio: true,
      //选中关闭
      clickClose: true,
      toolbar: {
        show: true,
        list: ['CLEAR']
      },
      filterable: true,
      //树
      tree: {
        show: true,
        //非严格模式
        strict: false,
        //展开所有节点
        expandedKeys: true,
      },
      data: []
    });

    admin.req({
      url: '/organization/getOrganizationSelectList',
      type: 'post',
      done: function (res) {
        orgCode_select.update({
          data: res.data,
          autoRow: true,
        });
        //异步远程加载数据，需要重新设置
        orgCode_select.changeExpandedKeys(true);
        if (orgCode) {
          orgCode_select.setValue([orgCode]);
        }
      }
    });

    //岗位
    let position_select = xmSelect.render({
      el: '#positions',
      name: 'positions',
      layVerify: 'required',
      layVerType: 'msg',
      disabled: true,
      toolbar: {
        show: true,
        list: ['ALL', 'CLEAR', 'REVERSE']
      },
      filterable: true,
      data: []
    });

    admin.req({
      url: '/position/getPositionSelectList',
      type: 'post',
      done: function (res) {
        position_select.update({
          data: res.data,
          autoRow: true,
        });
        if (positions) {
          position_select.setValue(positions);
        }
      }
    });

    //角色
    let role_select = xmSelect.render({
      el: '#roles',
      name: 'roles',
      layVerify: 'required',
      layVerType: 'msg',
      disabled: true,
      toolbar: {
        show: true,
        list: ['ALL', 'CLEAR', 'REVERSE']
      },
      filterable: true,
      data: []
    });

    admin.req({
      url: '/role/getRoleSelectList',
      type: 'post',
      done: function (res) {
        role_select.update({
          data: res.data,
          autoRow: true,
        });
        if (roles) {
          role_select.setValue(roles);
        }
      }
    });

    //提交
    form.on('submit(LAY-user-front-submit)', function (data) {
      let field = data.field;
      //不用修改岗位和所拥有的角色
      delete field.positions;
      delete field.roles;

      admin.req({
        url: '/user/save',
        type: 'post',
        contentType: 'application/json;charset=utf-8',
        dataType: 'json',
        data: JSON.stringify(field),
        done: function (res) {
          //执行成功
          let index = parent.layer.getFrameIndex(window.name);//获取当前弹出层的层级
          layer.msg(res.msg, {icon: 1, time: 500}, function () {
            parent.layer.close(index);//关闭弹出层
            parent.layui.admin.events.closeThisTabs();
          });
        }
      });
      // return false;
    });

    //重置表单 包含有下拉tree需要特殊处理
    layui.$("#LAY-user-front-reset").click(function () {
      layui.$("#user-form-edit")[0].reset();
      orgCode_select.setValue(orgCode ? [orgCode] : []);
      position_select.setValue(positions ? positions : []);
      role_select.setValue(roles ? roles : []);
      form.render();
    });

  });
</script>
</body>
</html>